package org.dromara.travel.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.travel.domain.bo.EventsBo;
import org.dromara.travel.domain.vo.EventsVo;
import org.dromara.travel.domain.Events;
import org.dromara.travel.mapper.EventsMapper;
import org.dromara.travel.service.IEventsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 活动Service业务层处理
 *
 * @author Jacheut
 * @date 2024-04-05
 */
@RequiredArgsConstructor
@Service
public class EventsServiceImpl implements IEventsService {

    private final EventsMapper baseMapper;

    /**
     * 查询活动
     */
    @Override
    public EventsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询活动列表
     */
    @Override
    public TableDataInfo<EventsVo> queryPageList(EventsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Events> lqw = buildQueryWrapper(bo);
        Page<EventsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询活动列表
     */
    @Override
    public List<EventsVo> queryList(EventsBo bo) {
        LambdaQueryWrapper<Events> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Events> buildQueryWrapper(EventsBo bo) {
        LambdaQueryWrapper<Events> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), Events::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), Events::getDescription, bo.getDescription());
        lqw.eq(bo.getLocationId() != null, Events::getLocationId, bo.getLocationId());
        lqw.eq(bo.getStartDate() != null, Events::getStartDate, bo.getStartDate());
        lqw.eq(bo.getEndDate() != null, Events::getEndDate, bo.getEndDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCover()), Events::getCover, bo.getCover());
        lqw.eq(bo.getCostMin() != null, Events::getCostMin, bo.getCostMin());
        lqw.eq(bo.getCostMax() != null, Events::getCostMax, bo.getCostMax());
        lqw.eq(bo.getRegistrationFeeFlag() != null, Events::getRegistrationFeeFlag, bo.getRegistrationFeeFlag());
        lqw.eq(bo.getRegistrationFee() != null, Events::getRegistrationFee, bo.getRegistrationFee());
        lqw.eq(bo.getCreatorId() != null, Events::getCreatorId, bo.getCreatorId());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), Events::getCategory, bo.getCategory());
        lqw.eq(bo.getEventType() != null, Events::getEventType, bo.getEventType());
        lqw.eq(bo.getIsApprove() != null, Events::getIsApprove, bo.getIsApprove());
        lqw.eq(StringUtils.isNotBlank(bo.getApproveMsg()), Events::getApproveMsg, bo.getApproveMsg());
        return lqw;
    }

    /**
     * 新增活动
     */
    @Override
    public Boolean insertByBo(EventsBo bo) {
        Events add = MapstructUtils.convert(bo, Events.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改活动
     */
    @Override
    public Boolean updateByBo(EventsBo bo) {
        Events update = MapstructUtils.convert(bo, Events.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Events entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除活动
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
